package com.dana.system.controller.sysuser;

import com.core.constants.HttpConstants;
import com.core.controller.BaseController;
import com.core.domain.Result;
import com.core.domain.vo.LoginUserVO;
import com.dana.system.domain.sysuser.dto.LoginDTO;
import com.dana.system.domain.sysuser.dto.SysUserSaveDTO;
import com.dana.system.domain.sysuser.vo.SysUserVO;
import com.dana.system.service.sysuser.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/sysUser")
@RestController
@Tag(name = "管理员接口")
@Slf4j
public class SysUserController extends BaseController {
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 登录
     * @Validated 用于开启对方法参数的校验功能
     * @RequestBody 将json转换成对象
     * @param loginDTO
     */
    @Operation(summary = "管理员登錄",description = "根据提供的信息新增管理员")
    @PostMapping("/login")
    public Result<String> login(@RequestBody
                                @Validated LoginDTO loginDTO){

        return sysUserService.login(loginDTO.getUserAccount(), loginDTO.getPassword());
    }


    /**
     * 添加管理员
     */
    @Operation(summary = "新增管理员",description = "根据提供的信息新增管理员")
    @RequestMapping("/add")
    public Result<Void> add(SysUserSaveDTO sysUserSaveDTO){
        return toResult(sysUserService.add(sysUserSaveDTO));
    }


    /**
     * 用户退出登录
     */
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        if (token==null){
            return Result.fail();
        }
        boolean logout = sysUserService.logout(token);
        return toResult(logout);
    }



    /**
     * 获取用户
     */
    @GetMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        if (token==null){
            return Result.fail();
        }
        return sysUserService.info(token);
    }


    private Boolean exist(String token){
        if (token==null){
            return false;
        }
        return true;
    }

    /**
     * 删除
     * @param userId
     * @return
     */
    @Operation(summary = "删除⽤⼾", description = "通过⽤⼾id删除⽤⼾")
    @Parameters(value = {
            @Parameter(name = "userId",in = ParameterIn.PATH,description = "用户id")
    })
    @DeleteMapping("/{userId}")
    public Result<Void> delete(@PathVariable Long userId){
        return null;
    }

    /**
     * 详细信息
     * @param userId
     * @param sex
     * @return
     */
    @Parameters(value = {
            @Parameter(name = "userId",in = ParameterIn.QUERY,description = "用户id"),
            @Parameter(name = "sex",in = ParameterIn.QUERY,description = "用户性别")
    })
    @GetMapping("/detail")
    public Result<SysUserVO> detail(Long userId, @RequestParam(required = false) String sex){
        return null;
    }
}
